<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdminController as Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request; 
use App\Model\{Admin,User,Config,Cat,Article}; 
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use App\Libraries\vendor\Sms;
use GeoIp2\Database\Reader;
use App\Libraries\vendor\SphinxClient;

/**
 * @menu_cat 分享
 * @icon cog
 * @sort 30
 */
class ShareController extends Controller
{
	public function __invoke(){
        return $this->index(func_get_args());
    }

    /**
     * @menu 拉新素材
     */
    public function add() {
    	if(request()->isMethod('post')) {
			$this->validate(request(), [
				'content' => 'required',
				'pic'=>'nullable',
				'type'=>'required',
			]);

    		$param = request()->only(['content','pic','share_number','time','type','status']);
    		$param['time'] = time();
    		$id = DB::table('circle_material')->insertGetId($param);
    		if($id) {
    			$comment  = request()->only(['comment_content']);
    			DB::table('circle_comment')->insert([
    				'circle_id'=>$id,
    				'content'=>$comment['comment_content'],
    				'time'=>time(),
    				'status'=>1, //$param['status']
    			]);
    		}
 
    	}

    	return view('Admin.Share.add')->with([
            'cat'=>Config::get_config_bykey('circle_material_type'),
        ]);
    }

    public function edit($id) {
    	if(request()->isMethod('post')) {
			$this->validate(request(), [
				'content' => 'required',
				'pic'=>'nullable',
				'type'=>'required',
			]);

    		$param = request()->only(['content','pic','share_number','time','type','status']);
    		$param['time'] = time();
    		if(empty($param['pic'])) {
    			unset($param['pic']);
    		}else{
    			$data = DB::table('circle_material')->where('id',$id)->first();
    			if(!empty($data->pic)) {
    				$param['pic'] = $param['pic']. '|' .$data->pic;
    			}
    		}
    		$id = DB::table('circle_material')->where('id',$id)->update($param);

    		$has = DB::table('circle_comment')->where('circle_id',$id)->exists();

    		$comment  = request()->only(['comment_content']);
    		if($has) {
    			DB::table('circle_comment')->where('circle_id',$id)->update([
    				'content'=>$comment['comment_content'],
    				'status'=>1, //$param['status']
    			]);
    		}else{
    			DB::table('circle_comment')->insert([
    				'circle_id'=>$id,
    				'content'=>$comment['comment_content'],
    				'time'=>time(),
    				'status'=>1, //$param['status']
    			]);
    		}
    	}

    	$data = DB::table('circle_material')->where('id',$id)->first();
    	$data->pic = explode('|', $data->pic);

    	$comment = DB::table('circle_comment')->where('circle_id',$id)->first();
    	return view('Admin.Share.edit')->with([
            'cat'=>Config::get_config_bykey('circle_material_type'),
            'data'=>$data,
            'comment'=>$comment
        ]);
    }

    /**
     * @menu 所有数据
     */
    public function index() {
    	$list = DB::table('circle_material');
        $this->_search($list, $search);
        $total = $list->count();
        $this->_order($list, ['id'=>'desc']);
        $list = $list->paginate(20);
        foreach($list->items() as $item) {
        	$item->pic = explode('|',$item->pic);
        }

        return view('Admin.Share.index')->with([
            'cat'=>Config::get_config_bykey('circle_material_type'),
            'list'=>$list
        ]);
    }

    public function ajax_delete_pic() {
    	$param = request()->only(['id', 'img']);
    	$data = DB::table('circle_material')->where('id',$param['id'])->first(['pic']);
    	$pic = explode('|',$data->pic);
    	$rs = [];
    	foreach($pic as $val) {
    		if($val!=$param['img']) {
    			$rs[] = $val;
    		}
    	}

    	$bool = DB::table('circle_material')->where('id',$param['id'])->update(['pic'=>join('|',$rs)]);
    	if($bool) {
    		return response()->json(['status'=>1]);
    	}
    	return response()->json(['status'=>0]);
    }
}